/*** 
 * @_______________#########_______________________ 
 * @______________############_____________________ 
 * @______________#############____________________ 
 * @_____________##__###########___________________ 
 * @____________###__######_#####__________________ 
 * @____________###_#######___####_________________ 
 * @___________###__##########_####________________ 
 * @__________####__###########_####_______________ 
 * @________#####___###########__#####_____________ 
 * @_______######___###_########___#####___________ 
 * @_______#####___###___########___######_________ 
 * @______######___###__###########___######_______ 
 * @_____######___####_##############__######______ 
 * @____#######__#####################_#######_____ 
 * @____#######__##############################____ 
 * @___#######__######_#################_#######___ 
 * @___#######__######_######_#########___######___ 
 * @___#######____##__######___######_____######___ 
 * @___#######________######____#####_____#####____ 
 * @____######________#####_____#####_____####_____ 
 * @_____#####________####______#####_____###______ 
 * @______#####______;###________###______#________ 
 * @________##_______####________####______________ 
 * @
 * @Author: ipk518 121206530@qq.com
 * @Date: 2025-02-11 11:50:22
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-08-19 15:34:36
 * @FilePath: /etws-sxk2307/inc/autoTbf.h
 * @Description: 
 * @************佛祖保佑************
 * @Copyright (c) 2025 by etws@quyujiang, All Rights Reserved. 
 */

#ifndef _AUTO_TBF_
#define _AUTO_TBF_

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <iostream>
#include "Common.h"
#include "autoCalibration.h"
#include <mutex>
#include <condition_variable>

using namespace std;



class queueShare;
class shareSub;


//本地保存的TBF基准幅相结构体
struct __attribute__ ((__packed__)) tr_mulit_freq_tbf{
        uint16_t SendAmp[2][32];
        uint16_t SendPhase[2][32];
};

class autoTbf : public shareSub
{
public:
    // friend class Uio;
    autoTbf();
    ~autoTbf();
public:
    void update(int m_flag,vector<uint8_t>& dataIn) override;
    void test(char* m,int len) override;
public:
    tr_tbf_t tr_Cache;

    bool m_RunFlag;
    std::thread *taskFd;/*定时线程线程*/
    static void task(autoTbf *argc);//定时任务
    int g_manualCnt;//循环计数

    int g_manualAmpCnt = 0;//手动补平计数
    int g_manualPhaCnt = 0;
    uint16_t g_usSendData[2][64];//数据缓存

    
    //任务流程
    taskprocess taskProcess;
    taskprocess taskProcess1;
    int taskFreq;
    int taskBeamNum;
public:
    void init();
    void task1();
    void task2();

    void send(uioMode to);
    void send(uioMode to,unsigned char *dataOut,unsigned int dataLen);
    
    bool getGps(unsigned long dueTime);
    void startCali(uioMode st);
    bool setFreq(bool enable);
    bool startOperate();
    void setOperate(bool ret);
    void clearTBF();
    int tbf_readCouplingParameters();

    uioMode calcOffsetValue2(uioMode st,unsigned int freq, int apMode);
    void subtractCouplingCoefficient(uioMode st, unsigned int freq, uint16_t* calibrationData) ;
    void getCurrentCalibration(uioMode st,unsigned char *dataOut,int len);
    void updateAmplitudeOffset(uioMode st, unsigned int freq,const uint16_t* srcData,const uint16_t *standAmpData) ;
    void updatePhaseOffset(uioMode st, unsigned int freq,const uint16_t* srcData,const uint16_t *standAmpData) ;
private:
    //用于自动和手动校准
    class queueShare *m_hSendCaliQueue;
    class queueShare *m_vSendCaliQueue;
    class queueShare *m_hRevCaliQueue;
    class queueShare *m_vRevCaliQueue;
    
    std::mutex g_CaliMutex;
    std::condition_variable cv;

    int m_frequeuce;
    struct ap_update_t webCache_H;//用于上报雷象上位机
    struct ap_update_t webCache_V;//用于上报雷象上位机
};

extern struct tr_mulit_freq_tbf tbf_mulitFreqStr[64];

extern tr_tbf_beam_t tbfCacheStr_H;
extern tr_tbf_beam_t tbfCacheStr_V;
extern tr_tbf_beam_t tbfSendStr;

#endif // !_AUTO_TBF_


